/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  OkHttpConfiguration.java   
 * @Package com.axin.common.huobi   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年3月9日 下午11:42:25   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.common.huobi;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;

/**   
 * @Description: TODO 
 * @ClassName:  OkHttpConfiguration
 * @author:  Axin 
 * @date:   2019年3月9日 下午11:42:25   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
@Configuration
public class OkHttpConfiguration {

	private final int CONN_TIMEOUT = 60;
    private final int READ_TIMEOUT = 60;
    private final int WRITE_TIMEOUT = 60;
    /**是否使用代理ip,因API限境外IP调用，本地调试时，设 为true*/
    private final boolean isProxy = false;
    /**代理IP：境外代理IP，当isProxy为true时有效*/
    private final String proxyHostName = "104.129.196.165";
    /**代理端口：境外代理IP访问端口，当isProxy为true时有效*/
    private final int proxyPort = 10605;
    
    @Bean
    public X509TrustManager x509TrustManager() {
        return new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
    }
    @Bean
    public SSLSocketFactory sslSocketFactory() {
        try {
            //信任任何链接
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * Create a new connection pool with tuning parameters appropriate for a single-user application.
     * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently
     */
    @Bean
    public ConnectionPool pool() {
        return new ConnectionPool(200, 50, TimeUnit.MINUTES);
    }

    
    @Bean
    public OkHttpClient createOkHttpClient() {
       /*  new Builder()
        		.sslSocketFactory(sslSocketFactory(), x509TrustManager())
        		.retryOnConnectionFailure(false)//是否开启缓存
        		.connectionPool(pool())//连接池
        		.connectTimeout(CONN_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                .proxy(
                		new Proxy(Proxy.Type.HTTP, new InetSocketAddress("5.135.164.72", 3128)))
                .build();*/
         Builder sslSocketFactory = new Builder()
        		 			.sslSocketFactory(sslSocketFactory(), x509TrustManager())
        		 			.retryOnConnectionFailure(false)//是否开启缓存
        	        		.connectionPool(pool())//连接池
        	        		.connectTimeout(CONN_TIMEOUT, TimeUnit.SECONDS)
        	                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
        	                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS);
         if(isProxy)
        	 sslSocketFactory.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHostName, proxyPort)));
         
         return sslSocketFactory.build();
    }
    
    
}
